let express = require('express');
const cors = require('cors');
const fs = require('fs');
//创建一个服务
let server = express();

server.listen('66',()=>{
    console.log('监听端口成功!');
})
//请求静态资源文件


//实现跨域
server.use(cors());

//设置api:getUsers返回所有用户的信息

server.use(function (req, res, next) {
    console.log(req.path);
    if(req.path === '/getUsers'){
        res.set('Content-Type', 'text/json');
        res.sendFile('C:/Users/30313/Desktop/crm客户挂历系统后台/client/json/users.json')
        return;
    }
 //设置api：getCustoms返回所有客户信息
    if(req.path === '/getCustoms'){
        res.set('Content-Type', 'text/json');
        res.sendFile('C:/Users/30313/Desktop/crm客户挂历系统后台/client/json/customs.json')
        return;
    }
    //设置api:getTasks返回所有任务的信息
    if(req.path === '/getTasks'){
        res.set('Content-Type', 'text/json');
        res.sendFile('C:/Users/30313/Desktop/crm客户挂历系统后台/client/json/tasks.json')
        return;
    }
    //设置api:addTasks增加任务信息
    if(req.path === '/addTasks'){
        console.log('111');
        res.set('Content-Type', 'text/json');
        let taskdata = fs.readFileSync('C:/Users/30313/Desktop/crm客户挂历系统后台/client/json/tasks.json','utf-8')
        taskdata = JSON.parse(taskdata);
        //新任务
        let newTask = req.query;
        let maxId = 0;
        taskdata.forEach((item=>{
            if(parseFloat(item.id) > maxId){
                maxId = parseFloat(item.id);
            }
        }))
        newTask.id = maxId + 1;
        newTask.state = "未完成";
        taskdata.push(newTask);
        fs.writeFileSync('C:/Users/30313/Desktop/crm客户挂历系统后台/client/json/tasks.json',JSON.stringify(taskdata))
        res.sendFile('C:/Users/30313/Desktop/crm客户挂历系统后台/client/json/tasks.json')
        console.log(taskdata,newTask);
        return;
    }
    //设置api：修改任务的状态
    if(req.path === '/changeTasks'){
        res.set('Content-Type', 'text/json');
        let taskdata = fs.readFileSync('C:/Users/30313/Desktop/crm客户挂历系统后台/client/json/tasks.json','utf-8');
        taskdata = JSON.parse(taskdata);
        //改变的信息id
        let changeData = req.query;
        console.log(changeData);
        taskdata.forEach((item)=>{
            if(item.id == parseFloat(changeData.id) + 1 ){
                item.state = '已完成';
            }
        })

        fs.writeFileSync('C:/Users/30313/Desktop/crm客户挂历系统后台/client/json/tasks.json',JSON.stringify(taskdata));

    }
    //设置api:删除任务
    if(req.path === '/deleteTasks'){
        res.set('Content-Type', 'text/json');
        let taskdata = fs.readFileSync('C:/Users/30313/Desktop/crm客户挂历系统后台/client/json/tasks.json','utf-8');
        taskdata = JSON.parse(taskdata);
        //改变的信息id
        let changeData = req.query;
        console.log(changeData);
        taskdata = taskdata.filter((item)=>{
            return parseFloat(item.id) != parseFloat(changeData.id);
        })
        console.log(taskdata);
        fs.writeFileSync('C:/Users/30313/Desktop/crm客户挂历系统后台/client/json/tasks.json',JSON.stringify(taskdata));
        res.sendFile('C:/Users/30313/Desktop/crm客户挂历系统后台/client/json/tasks.json')


    }
    //
    


  })

 
  